package com.zmt.mrms.service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import org.springframework.stereotype.Service;

import com.zmt.mrms.entity.HospitalListPrice;
import com.zmt.mrms.entity.HospitalSection;
import com.zmt.mrms.entity.MedicalRecord;
import com.zmt.mrms.entity.PaymentRecord;
import com.zmt.mrms.entity.PrescriptionRecord;
import com.zmt.mrms.entity.UserPatient;
import com.zmt.mrms.mapper.PatientMapper;
import com.zmt.mrms.utils.FormatterUtils;
import com.zmt.mrms.utils.VerifyUtils;

import jakarta.annotation.Resource;

@Service
public class PatientService {

	@Resource
	private PatientMapper patientMapper;

	VerifyUtils verifyTool = new VerifyUtils();
	FormatterUtils formatterUtils = new FormatterUtils();

	public List<PrescriptionRecord> getPreRecordbyIdcard(String idcard) {
		List<MedicalRecord> list = patientMapper.findRecordByIdcard(idcard);
		List<PrescriptionRecord> preRecord = new ArrayList<>();
		for (int i = 0; i < list.size(); i++) {
			int medRecordId = list.get(i).getId();
			preRecord.add(patientMapper.findPreRecordByMedRecId(medRecordId));
		}
		return preRecord;
	}

	public List<PrescriptionRecord> getPreRecordbyMedId(int id) {
		PrescriptionRecord record = patientMapper.findPreRecordByMedRecId(id);
		List<PrescriptionRecord> preRecord = new ArrayList<>();
		preRecord.add(record);
		return preRecord;
	}

	public List<PrescriptionRecord> getPatientAllPreRecord(int id) {
		List<MedicalRecord> list = patientMapper.findRecordByPatientId(id);
		List<PrescriptionRecord> preRecord = new ArrayList<>();
		for (int i = 0; i < list.size(); i++) {
			int medRecordId = list.get(i).getId();
			preRecord.add(patientMapper.findPreRecordByMedRecId(medRecordId));
		}
		return preRecord;
	}

	public List<MedicalRecord> getMedRecordbyMedId(int id) {
		List<MedicalRecord> list = new ArrayList<>();
		list.add(patientMapper.findRecordByMedicalId(id));
		return list;
	}

	public List<MedicalRecord> getMedRecordbyIdcard(String idcard) {
		return patientMapper.findRecordByIdcard(idcard);
	}

	public List<MedicalRecord> getPatientAllMedRecords(int id) {
		return patientMapper.findRecordByPatientId(id);
	}

	public String payForBill(String billType, int paymentId) {
		patientMapper.payBill(billType, paymentId);
		return "success";
	}

	public PaymentRecord getPayment(int id) {
		return patientMapper.getPayment(id);
	}

	public String doRegistered(MedicalRecord medicalRecord) {
		UserPatient userPatient = patientMapper.findbyIDCard(medicalRecord.getPatientIdcard());
		if (userPatient != null) {
			medicalRecord.setPatientId(userPatient.getId());
		}
		medicalRecord.setCreateTime(formatterUtils.nowDateTime());
		patientMapper.createMedicalRecord(medicalRecord);
		HospitalListPrice registeredPrice = patientMapper.registeredPrice(medicalRecord.getRegisteredType());
		PaymentRecord paymentRecord = new PaymentRecord();
		paymentRecord.setMedicalRecordId(medicalRecord.getId());
		paymentRecord.setRegisteredCost(registeredPrice.getPrice());
		paymentRecord.setRegisteredStatus(false);
		patientMapper.createPaymentRecord(paymentRecord);
		return Integer.toString(paymentRecord.getId());
	}

	public Map<String, String> getPersonalInfo(String id) {
		UserPatient userPatient = patientMapper.findbyId(Integer.parseInt(id));
		Map<String, String> map = new HashMap<>();
		map.put("name", userPatient.getName());
		map.put("sex", userPatient.getSex());
		String age = verifyTool.getAgeForBirthday(userPatient.getBirthday());
		map.put("age", age);
		map.put("idcard", userPatient.getIdentifyCard());
//		System.out.println(map);
		return map;
	}

	public Map<String, Set<String>> getSectionList() {
		Map<String, Set<String>> hospitalSection = new HashMap<>();
		List<HospitalSection> list = patientMapper.selectAllSection();
		TreeSet<String> sectionTypeSet = new TreeSet<>();
		for (int i = 0; i < list.size(); i++) {
			sectionTypeSet.add(list.get(i).getType());
		}
		hospitalSection.put("type", sectionTypeSet);
		Iterator<String> iterator = sectionTypeSet.iterator();
		while (iterator.hasNext()) {
			TreeSet<String> set = new TreeSet<>();
			String typeString = iterator.next();
			for (int i = 0; i < list.size(); i++) {
				if (typeString.equals(list.get(i).getType())) {
					set.add(list.get(i).getName());
				}
			}
			hospitalSection.put(typeString, set);
		}
		return hospitalSection;
	}

	public String updatePersonalInfo(UserPatient userPatient) throws ParseException {
		if (!verifyTool.isValidIDCard(userPatient.getIdentifyCard())) {
			return "请输入有效的身份证号码";
		} else {
			try {
				patientMapper.updatePersonalInfo(userPatient);
				return "success";
			} catch (Exception e) {
				// TODO: handle exception
				return "error";
			}
		}
	}

	public String updateAccountInfo(UserPatient userPatient) {
		if (!verifyTool.isValidEmail(userPatient.getEmail())) {
			return "请输入有效的邮箱";
		} else if (!verifyTool.isValidPwd(userPatient.getPassword())) {
			return "需设置8-20位之间的密码，并且必须包含大小写字母和数字三种类型";
		} else {
			try {
				patientMapper.updateAccountInfo(userPatient);
				return "success";
			} catch (Exception e) {
				// TODO: handle exception
				return "error";
			}
		}
	}

	public UserPatient findPatient(String id) {
		return patientMapper.findbyId(Integer.parseInt(id));
	}

	public String Register(String email, String pwd) {
		if (email.length() == 0 || pwd.length() == 0) {
			return "请完善您的注册表单";
		} else if (!verifyTool.isValidEmail(email)) {
			return "请输入有效的邮箱";
		} else if (!verifyTool.isValidPwd(pwd)) {
			return "需设置8-20位之间的密码，并且必须包含大小写字母和数字三种类型";
		} else {
			UserPatient userPatient = patientMapper.findbyEmail(email);
			if (userPatient != null) {
				return "该邮箱已被注册，请重试";
			} else {
				patientMapper.createAccountbyEmail(email, pwd, "PATIENT");
				return "success";
			}

		}
	}

	public UserPatient Login(String account, String pwd) {
		if (verifyTool.isValidEmail(account)) {
//			System.out.println("走的邮箱");
			return patientMapper.loginbyEmail(account, pwd);
		} else {
//			System.out.println("走的手机");
			return patientMapper.loginbyPhone(account, pwd);
		}

	}

}
